package com.dxysun.sql.springjdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.dxysun.sql.domain.User;

@Repository
public class UserDao
{

	@Autowired
	private JdbcTemplate template;

	public void addUser()
	{
		int count = template.update("insert into dxy_test.t_user(user_name,real_name)values(?,?)", "bobo", "波波老师");
		System.out.println("count = " + count);
	}

	public void query1()
	{
		String sql = "select * from dxy_test.t_user";
		List<User> list = template.query(sql, new RowMapper<User>() {
			@Override
			public User mapRow(ResultSet rs, int rowNum) throws SQLException
			{
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setUserName(rs.getString("user_name"));
				user.setRealName(rs.getString("real_name"));
				return user;
			}
		});
		for (User user : list)
		{
			System.out.println(user);
		}
	}

	public void query2()
	{
		String sql = "select * from dxy_test.t_user";
		List<User> list = template.query(sql, new BeanPropertyRowMapper<>(User.class));
		for (User user : list)
		{
			System.out.println(user);
		}
	}

}
